package com.glut.campusrepair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.glut.campusrepair.entity.User;
import com.glut.campusrepair.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/users")
public class UserController {


    // UserController.java
    @GetMapping("/page")
    public IPage<User> getUsersByPage(@RequestParam Integer current, @RequestParam Integer size) {
        IPage<User> page = new Page<>(current, size);
        return userService.page(page);
    }




    @Autowired
    private UserService userService;

    /**
     * 获取所有用户
     * @return 用户列表
     */
    @GetMapping
    public List<User> getAllUsers() {
        return userService.list();
    }

    /**
     * 添加用户
     * @param user 用户实体
     * @return 是否添加成功
     */
    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);
    }

    /**
     * 更新用户
     * @param user 用户实体
     * @return 是否更新成功
     */
    @PutMapping
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    /**
     * 删除用户
     * @param id 用户ID
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Long id) {
        return userService.removeById(id);
    }
}
